package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";
    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model) {
        //先根据结算单查询结算单对象
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        //根据结算单对象状态决定跳转到什么界面
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "itemEdit";
        } else {
            return prefix + "itemDetail";
        }
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(StatementItemQuery qo) {
        List list = statementItemService.query(qo);

        return new TablePageInfo(list);
    }

    @RequestMapping("/saveItems")
    @ResponseBody            //@RequestBody 接收前台传过来的JSON数据 贴此标签
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }
    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId) {
        statementItemService.pay(statementId);
        return AjaxResult.success();
    }

    @RequestMapping("/export")
    @ResponseBody       //不找视图了,然后请求是交给response进行响应
    public void export(HttpServletResponse response,Long statementId) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=statementData.xlsx");
        //把数据放到Workbook
        Workbook wb = statementService.export(statementId);
        //将内存中Workbook写到浏览器中
        wb.write(response.getOutputStream());
    }
}
